#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>

#include "../server.h"

int currentMode = 0;

void *transmitDmx();
void *changeValues();

unsigned char buffer[513];
int red, green, blue, fd;

//int mode;

void fixedMode()
{
	buffer[0] = 0;        //dit MOET 0 zijn
        buffer[1] = red;      //R strip 1
	buffer[2] = green;      //G strip 1
        buffer[3] = blue;      //B strip 1
	buffer[4] = 255;      //M strip 1
        buffer[5] = red;      //R strip 2
        buffer[6] = green;      //G strip 2
        buffer[7] = blue;      //B strip 2
        buffer[8] = 255;      //M strip 2

        write(fd, buffer, sizeof(buffer));
}

void *transmit_dmx()
{
    //int fd;
    //printf("Sending DMX \n");
    usleep(50000);
    fd = open("/dev/dmx0", O_WRONLY);
    if (fd < 0) {
	perror("/dev/dmx0 not found or busy");
	exit(-1);
    }
    while (1){
	//printf("cm:%d m:%d\n", currentMode, dmxMode);
	//usleep(50000);
	if(dmxMode != currentMode)
	{
	    switch( dmxMode )
	    {
		case 2:
		    printf("DMX:	changing mode: exit\n");
		    currentMode = dmxMode;
		    break;
		case 0:
		    printf("DMX:	changing mode: fixed\n");
		    currentMode = dmxMode;
		    break;
		case 1:
		    printf("DMX:	changing mode: slow fade\n");
		    currentMode = dmxMode;
		    break;
	    }
	}
	switch( currentMode )
	{
	    case 2:
		exit(0);
		break;
	    case 0:
		fixedMode();
		break;
	    case 1:
		fixedMode();
		break;
	}
    }                      
}
